<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :model="filters" :inline="true">
				<el-form-item>
					<el-input v-model="filters.keywords" placeholder="关键字" ></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getTenants">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表v-loading="listLoading"-->
		<el-table :data="tenants" v-loading="listLoading" highlight-current-row  style="width: 100%;">
			<!--多选框-->
			<el-table-column type="selection" width="55">
			</el-table-column>
			<!--索引值,为什么不用id,id不序号-->
			<el-table-column type="index" width="60">
			</el-table-column>
			<!--其他都设置值,只有一个不设置值就自动适应了-->
			<el-table-column prop="companyName" label="机构名称">
			</el-table-column>
			<el-table-column prop="tenantType.name" label="机构类型">
			</el-table-column>
			<el-table-column prop="employee.username" label="超级管理员">
			</el-table-column>
			<el-table-column prop="companyNum" label="营业执照">
			</el-table-column>
			<el-table-column prop="address" label="公司地址">
			</el-table-column>
			<el-table-column prop="logo" label="LOGO">

			</el-table-column>
			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button size="small"  @click="edit(scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="del(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"  :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--添加和修改界面-->
		<el-dialog :title="title" :visible.sync="addFormVisible">
			<el-form :model="addForm" width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="机构名称" prop="name">
					<el-input v-model="addForm.companyName" ></el-input>
				</el-form-item>
				<el-form-item label="机构类型" prop="sn">
                    <el-select v-model="addForm.tenantTypeId" >
                        <el-option
                                v-for="item in tenantTypes"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
				</el-form-item>
				<el-form-item label="超级管理员" prop="managerId">
                    <el-select v-model="addForm.employeeId" >
                        <el-option
                                v-for="item in employees"
                                :key="item.id"
                                :label="item.username"
                                :value="item.id">
                        </el-option>
                    </el-select>
				</el-form-item>
				<el-form-item label="营业执照" prop="managerId">
                    <el-input v-model="addForm.companyNum" ></el-input>
				</el-form-item>
				<el-form-item label="公司地址" prop="managerId">
                    <el-input v-model="addForm.address" ></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	export default {
		data() {
			return {
                listLoading:false,
				//查询对象
				filters:{
					keywords:''
				},
				page:1,//当前页,要传递到后台的
				total:0, //分页总数
			    tenants:[], //当前页数据
                addFormVisible:false,//默认不弹出
                tenantTypes: [], //所有机构类型
                employees: [], //所有的管理员
                title: '',
                //添加的数据
                addForm:{
                    id:'',
                    companyName:'',
                    tenantTypeId: '',
                    employeeId:'1',
                    companyNum: '',
                    address:'',
                },
                addFormRules: {  //规则
                    companyName: [
                        { required: true, message: '请填写机构名称', trigger: 'blur' }
                    ],
                    tenantTypeId: [
                        { required: true, message: '请填写机构类型', trigger: 'blur' }
                    ],
                    employeeId: [
                        { required: true, message: '请选择部门经理', trigger: 'blur' }
                    ]
                }
			}
		},
		methods: {
            //提交数据
            addSubmit(){
                // managerId: 329
                //添加一个对象 manager
                this.addForm["manager"]={"id":this.addForm.managerId};
                //表单的规则
                this.$refs["addForm"].validate((valid) => {
                    //如果符合规则就提交
                    if (valid) {
                        this.$http.put("/system/tenant", this.addForm).then(res => {
                            //console.debug(res)
                            let {success, msg} = res.data;
                            if (success) {
                                this.$message({
                                    message: this.title + '成功',
                                    type: 'success'
                                });
                                //关闭弹框
                                this.addFormVisible = false;
                                //重新加载
                                this.getTenants();
                            }
                        }).catch()
                    } else {
                        //console.log('error submit!!');
                        return false;
                    }
                })


            },
            //查询所有管理员
            getemployees(){
                this.$http.get("/system/employee").then(res=>{
                    //console.debug(res);
                    this.employees=res.data;
                })
            },
		    //查询所有的类型
		    gettenantTypes(){
                this.$http.get("/system/tenantType").then(res=>{
                    //console.debug(res);
                    this.tenantTypes=res.data;
                })
            },
            //添加
            handleAdd(){
                //判断有没有 如果有就清空
                if(this.$refs["addForm"]){
                    //清空规则
                    this.$refs["addForm"].resetFields();
                }
                this.title='新增';
                //查找所有机构类型
                this.gettenantTypes();
                //管理员查询
                this.getemployees();
                //清空表单
                for(var v in this.addForm){
                    this.addForm[v]=''
                }
                //弹出添加页面
                this.addFormVisible=true;
            },
            handleCurrentChange(curentPage){
                this.page = curentPage;
                this.getTenants();
			},
            getTenants(){
                //发送Ajax请求后台获取数据  axios
				//添加分页条件及高级查询条件
				let para = {
				    "page":this.page,
					"keyword":this.filters.keywords
				};
				this.listLoading = true; //显示加载圈
				//分页查询
                this.$http.post("/system/tenant/list",para) //$.Post(.....)
                    .then(result=>{
                        this.total = result.data.total;
                        this.tenants = result.data.rows;
                        this.listLoading = false;  //关闭加载圈
                    });
				/* 查询所有
				this.$http.patch("/department",para) //$.Post(.....)
					.then(result=>{
                        console.log(result);
                        console.log(result.data);
                        this.tenants = result.data;
                        this.listLoading = false;  //关闭加载圈
                    });
                    */
			}
		},
		mounted() {
		    this.getTenants()
		}
	}

</script>

<style scoped>

</style>